#! /usr/bin/env python

import pygame
import os
import inputbox

# Races:
# 0 - None, 1 - Dwarf, 2 - Elf, 3 - Halfling, 4 - Human, 5-Halfbreed

class Race(pygame.sprite.Sprite):
    def __init__(self, card_id, master_container):
        pygame.sprite.Sprite.__init__(self)

        self.card_id          = card_id #What card is called
        self.title            = ''      #What the user sees
        self.type             = 'race'  #What type of card is this
        self.base_image       = ''      #Image of the card - doesnt change
        self.image            = ''      #Cards image, changes if on playfield or hand
        self.zoomed_image     = ''      #Cards image that shows in bottom right of screen
        self.rect             = ''      #The bounding rectangle of the card, changes
        self.zoomed_rect      = ''      #Bounding rectangle for bottom right of screen
        self.x                = 0       #Top left x cornor used for positioning
        self.y                = 0       #Top left y cornor used for positioning
        self.rightx           = 0       #Right most x, used for boundary
        self.bottomy          = 0       #Bottom most y, used for boundary
        self.zoomed_x         = 963     #Top left x of the zoomed image
        self.zoomed_y         = 627     #Top left y of the zoomed image
        self.player           = 0       #Player that controls card
        self.mouse_over_card  = False   #Is mouse over card
        self.location         = 0
        self.master_container = master_container # Master container holds all containers used in the game. Allows card to move from one container to another.
        #Link to the screen for drawing
        self.screen = self.master_container[22]

        # Set up this particular card
        self.SetupCard()

    def AddCardToPlayfield (self):
        if self.player == 1:
            self.master_container[4].AddCard (self)
        elif self.player == 2:
            self.master_container[7].AddCard (self)
        elif self.player == 3:
            self.master_container[10].AddCard (self)
        elif self.player == 4:
            self.master_container[13].AddCard (self)
        elif self.player == 5:
            self.master_container[16].AddCard (self)
        else:
            self.master_container[19].AddCard (self)

    def CheckPlayingSameCard (self):
        # Checks if the player is trying to play a race card he or she already has
        r = False

        if self.player == 1:
            t = self.master_container[4]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        elif self.player == 2:
            t = self.master_container[7]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        elif self.player == 3:
            t = self.master_container[10]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        elif self.player == 4:
            t = self.master_container[13]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        elif self.player == 5:
            t = self.master_container[16]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        elif self.player == 6:
            t = self.master_container[19]
            if t.CheckRaceOneTitle() == self.title:
                r = True
                pass
            elif t.CheckRaceTwoTitle() == self.title:
                r = True
                pass
        return r
        

    def ClickedOnCard (self):
        # Temporarily using the command console for all input.
        print 'Do you wish to play "%s"? {y} or {n}: ' % (self.title)
        inp = raw_input (": ")
        if inp == 'y':
            self.PlayCard ()
##        # What happens with user interaction?
##        ans = inputbox.ask (self.screen, "Want to play " + self.title + "? (y or n): ")
##        if ans == 'y':
##            # Try to play card
##            self.PlayCard ()
##        else:
##            # Do not play card
##            pass

    def DoesPlayerHaveRace (self):
        # This function checks to see if the player has the maximum number of races allowed dependent upon
        # whether he or she has a halfbreed card in play.  If the play does have the maximum already, then
        # True is returned.  if the player does NOT have the maximum allowed, then False is returned.
        num_races = 0
        halfbreed = False

        if self.player == 1:
            t = self.master_container[4]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()
        elif self.player == 2:
            t = self.master_container[7]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()
        elif self.player == 3:
            t = self.master_container[10]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()
        elif self.player == 4:
            t = self.master_container[13]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()
        elif self.player == 5:
            t = self.master_container[16]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()
        elif self.player == 6:
            t = self.master_container[19]
            num_races = t.CheckNumRaces()
            halfbreed = t.CheckForHalfbreed ()

        if num_races == 0:
            return False
        elif num_races == 1:
            if halfbreed:
                return False
            else:
                return True
        else:
            return True

    def DoesPlayerHaveHalfbreed (self):
        # This function checks to see if the player is a halfbreed
        h = False

        if self.player == 1:
            h = self.master_container[4].CheckForHalfbreed ()
        elif self.player == 2:
            h = self.master_container[7].CheckForHalfbreed ()
        elif self.player == 3:
            h = self.master_container[10].CheckForHalfbreed ()
        elif self.player == 4:
            h = self.master_container[13].CheckForHalfbreed ()
        elif self.player == 5:
            h = self.master_container[16].CheckForHalfbreed ()
        elif self.player == 6:
            h = self.master_container[19].CheckForHalfbreed ()
        return h

    def PlayCard (self):
        # This function first sees whether or not the player has the maximum
        # allowed number of races already by calling DoesPlayerHaveRace.
        # If the function returns False, then that player has room for another
        # race card.  If it returns true, the players must discard a card.
        play = False

        # Check first if the player already has this card in play
        if self.CheckPlayingSameCard() == True:
            print '\nAlready are a "%s". Not allowing play of duplicate race card.\n' % (self.title)

        else:
            if self.title != 'Half-Breed':
                if self.player == 1:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        # need to get rid of a race
##                        answer = inputbox.ask (self.screen, "Already have max number of allowed races." +
##                                            "  Do you still wish to play card? (y or n): ")
                        # New section using command console
                        answer = raw_input ('Maximum number of race cards reached. Do you wish to remove a card' +
                        ' from play? {y} or {n}: ')
                        if answer == 'y':
                            # Remove a race card from play first.
                            self.master_container[4].PickRaceCardToDiscard ()
                            play = True
                        else:
                            # User does not want to discard a card. Do nothing.
                            print 'You chose to not play this race card.'
                    else:
                        # ok to play card
                        play = True
                elif self.player == 2:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 3:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 4:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 5:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 6:
                    answer = self.DoesPlayerHaveRace ()
                    if answer:
                        pass
                    else:
                        play = True
            # Card is the half-breed card
            else:
                if self.player == 1:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        # Do not play card
                        print 'Already a Halfbreed. Not playing a duplicate copy.'
                    else:
                        # ok to play card
                        play = True
                elif self.player == 2:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 3:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 4:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 5:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        pass
                    else:
                        play = True
                elif self.player == 6:
                    answer = self.DoesPlayerHaveHalfbreed ()
                    if answer:
                        pass
                    else:
                        play = True
        if play:
            self.RemoveCardFromContainer ()
            self.AddCardToPlayfield ()

    def PrintData (self):
        # This function outputs the data of this particular card to the console
        # This is for testing purposes only.
        print '''
Card ID:    %s
Card Name:  %s
Card Type:  %s
Card Owner: %s
            \n''' % (self.card_id, self.title, self.type, self.player)
        

    def RemoveCardFromContainer (self):
        if self.location == 1:
            # Remove from player hand
            if self.player == 1:
                self.master_container[5].RemoveCard(self)
            elif self.player == 2:
                self.master_container[8].RemoveCard(self)
            elif self.player == 3:
                self.master_container[11].RemoveCard(self)
            elif self.player == 4:
                self.master_container[14].RemoveCard(self)
            elif self.player == 5:
                self.master_container[17].RemoveCard(self)
            else:
                self.master_container[20].RemoveCard(self)

        elif self.location == 2:
            # Remove from player playfield
            if self.player == 1:
                self.master_container[4].RemoveCard(self)
            elif self.player == 2:
                self.master_container[7].RemoveCard(self)
            elif self.player == 3:
                self.master_container[10].RemoveCard(self)
            elif self.player == 4:
                self.master_container[13].RemoveCard(self)
            elif self.player == 5:
                self.master_container[16].RemoveCard(self)
            else:
                self.master_container[19].RemoveCard(self)

    def SetLocation (self, location):
        self.location = location

    def SetupCard (self):
        #This function sets the cards variables.
        #Self id is called when the game starts. It creates
        #an instance for each card and places it in a 'deck'.

        if self.card_id == 'Dwarf1':
            self.title = 'Dwarf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'DwarfOne.png')).convert()
        elif self.card_id == 'Dwarf2':
            self.title = 'Dwarf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'DwarfTwo.png')).convert()
        elif self.card_id == 'Dwarf3':
            self.title = 'Dwarf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'DwarfThree.png')).convert()

        elif self.card_id == 'Elf1':
            self.title = 'Elf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'ElfOne.png')).convert()
        elif self.card_id == 'Elf2':
            self.title = 'Elf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'ElfTwo.png')).convert()
        elif self.card_id == 'Elf3':
            self.title = 'Elf'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'ElfThree.png')).convert()

        elif self.card_id == 'Halfling1':
            self.title = 'Halfling'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'HalflingOne.png')).convert()
        elif self.card_id == 'Halfling2':
            self.title = 'Halfling'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'HalflingTwo.png')).convert()
        elif self.card_id == 'Halfling3':
            self.title = 'Halfling'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'HalflingThree.png')).convert()

        elif self.card_id == 'Halfbreed1':
            self.title = 'Half-Breed'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'HalfbreedOne.png')).convert()
        elif self.card_id == 'Halfbreed2':
            self.title = 'Half-Breed'
            self.image = pygame.image.load(os.path.join('data/images/Cards/Races', 'HalfbreedTwo.png')).convert()

        self.base_image = self.image
        self.zoomed_image = pygame.transform.scale (self.image, (314, 394))
        self.image = pygame.transform.scale (self.image, (80,100))
        self.SetRect ()

    def SetRect (self):
        self.rect = self.image.get_rect ()
        self.zoomed_rect = self.zoomed_image.get_rect ()

    def SetRectTopLeft (self, (x, y)):
        # This function is called by the PlayerHands class.
        # It allows the hand to tell the card what position it is within
        # a player hand and thus be able to draw correctly when a player
        # is looking at his or her hand.
        self.x = x
        self.y = y
        if self.location == 1:
            self.image = pygame.transform.scale (self.base_image, (100,125))
            self.rightx = self.x + 100
            self.bottomy = self.y + 125
        else:
            self.image = pygame.transform.scale (self.base_image, (80, 100))
            self.rightx = self.x + 80
            self.bottomy = self.y + 100
        self.rect.topleft = (self.x, self.y)
        self.zoomed_rect.topleft = (self.zoomed_x, self.zoomed_y)

    def Update (self, mouse_x, mouse_y, clicked):
        # is mouse over the card?
        if ((mouse_x >= self.x) and (mouse_x <= self.rightx) and
            (mouse_y >= self.y)  and (mouse_y <= self.bottomy)):
            self.mouse_over = True

            if clicked:
                self.PrintData ()
                if self.player == 1:
                    if self.location == 1:
                        self.ClickedOnCard ()
                    elif self.location == 2:
                        pass

            # Draw sparkles
            s = self.master_container [23]
            s(self.x, self.rightx, self.y, self.bottomy)

        # Mouse is no longer hovering over the image of the card.
        else:
            self.mouse_over = False

        self.screen.blit (self.image, self.rect)

        if self.mouse_over:
            self.screen.blit (self.zoomed_image, self.zoomed_rect)
